package com.api.syh56.shengyunhui.controller.admin;

import com.api.syh56.shengyunhui.common.CommonPage;
import com.api.syh56.shengyunhui.common.CommonResult;
import com.api.syh56.shengyunhui.common.Constants;
import com.api.syh56.shengyunhui.entity.User;
import com.api.syh56.shengyunhui.service.AdminUserService;
import com.api.syh56.shengyunhui.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 用户管理
 */
@RestController
@RequestMapping("admin/user")
public class AdminUserController {
    @Autowired
    private AdminUserService adminUserService;

    @Autowired
    private UserService userService;


    /**
     * 查询所有专线用户
     */
    @GetMapping("getUser")
    public CommonResult<Object> getUser(){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getRoles, Constants.Role.DRIVER);
        List<User> list = userService.list(userQueryWrapper);
        List<String> telephone = new ArrayList<>();
        for (User user : list){
            String userName = user.getUserName();
            telephone.add(userName);
        }
        System.out.println(telephone.size());
        return CommonResult.success(telephone);
    }

    /**
     * 查询用户 手机号（精确） 昵称（模糊）
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("select")
    public CommonResult<CommonPage<User>> select(@RequestParam String roles,
                                                 @RequestParam String username,
                                                 @RequestParam String company,
                                                @RequestParam String inviteBy,
                                                @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){

        Short rolesShort = null;

        // 尝试将字符串转换为short
        if (roles.isEmpty()) {
            rolesShort = null; // 空字符串转换结果为null
        } else {
            rolesShort = Short.parseShort(roles);
        }
        Page<User> userPage = adminUserService.select(rolesShort,username,company,inviteBy,pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(userPage));
    }
    @PostMapping("update")
    public CommonResult<User> update(User user){
        User byId = userService.getById(user);
        if (byId!=null){
            userService.updateById(user);
            return CommonResult.success(user);
        }else {
            return CommonResult.failed("用户不存在");
        }
    }
    @GetMapping("updateRoles")
    public CommonResult<Object> updateRoles(@RequestParam  Long uid,@RequestParam Short roles) {
        User user = userService.getById(uid);

        HashSet<String> clearHashSet = userService.getClear(uid);
        String message = "";
        for (String s : clearHashSet) {
            message += s + ",";
        }
        if (clearHashSet.isEmpty()) {
            user.setRoles(roles);
            if (userService.updateById(user)) {
                return CommonResult.success(user);
            } else {
                return CommonResult.failed("更新失败");
            }
        } else {
            return CommonResult.failed(message);
        }
    }

}
